const Controller = require("uni-cloud-router").Controller;
const {validateToken} = require('validdate-token');
module.exports = class UserController extends Controller{
	/**
	 * @param {Object} token
	 * 验证token
	 */
	async _checkToken(token){
		let tokenRes;
		try{
			tokenRes = await validateToken(token);
			if(tokenRes.code !== 0){
				return{
					status:-1,
					msg:"稍后重试",
					errCode:tokenRes.errCode
				}
			} else {
				return {
					status:0,
					msg:"token有效",
				}
			}
		}catch(e){
			return {
			  status: -1,
			  errCode: 'TOKEN_INVALID',
			  msg: 'token无效'
			}
		}
	}
	/**
	 * 2020-08-01
	 * 获取用户信息
	 */
	async getUserInfo(){
		let {_userID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.user.getUserInfo({_userID});
		}
	}
	/**
	 * 2020-07-26
	 * 编辑用户信息
	 */
	async setUserInfo(){
		let {name,phone,address,detailAddre,idCard,bankID,wkID,userType,sex,_userID,password,workStatus,pm_company} = this.ctx.data;
		return this.service.user.setUserInfo({name,phone,address,detailAddre,idCard,bankID,wkID,userType,sex,_userID,password,workStatus,pm_company});
	}
	/**
	 * 2020-08-01
	 * 获取用户所属角色
	 */
	async getUserType(){
		let {userID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.user.getUserType({userID});
		}
	}
	
	/**
	 * 2021-3-29
	 * 根据员工ID（wkID）获取员工信息
	 */
	async getUserInfoByWkID(){
		let {wkID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.user.getUserInfoByWkID(wkID);
		}
	}
	
	/**
	 * 2020-3-29
	 * 项目经理绑定员工
	 */
	async pmBindWorker(){
		let {wkID,pmID,token,pm_company} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.user.pmBindWorker(wkID,pmID,pm_company);
		}
	}
	
	/**
	 * 2020-5-30
	 * 获取绑定的员工ID
	 */
	async getWorkerIDs(){
		let {userID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.user.getWorkerIDs(userID);
		}
	}
	/**
	 * 解除绑定员工
	 */
	async relieveBindWorker(){
		let {userID,token,wkID} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.user.relieveBindWorke(userID,wkID);
		}
	}
	/**
	 * 获取员工根据 公司ID
	 */
	async getUserDataByPmCompany(){
		let {token,pm_company} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.user.getUserDataByPmCompany(pm_company);
		}
	}
}